package br.com.fsa.aplication.Util;

import java.util.HashMap;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

import br.com.fsa.aplication.Exception.ConectionException;
import br.com.fsa.aplication.Model.LogisticaVO;
import br.com.fsa.aplication.Services.LogisticaService;


public class LogisticaConverter implements Converter {
@Override
	public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2)
	{
		try
		{
			if (arg2 != null && !arg2.trim().equals(""))
			{

				HashMap dto = new HashMap();
				dto.put("objeto", Long.parseLong(arg2));
				LogisticaService servico = new LogisticaService();
				return (LogisticaVO) servico.obterPorId(dto).get("resposta");

			}

		} catch (ConectionException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

@Override
	public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2)
	{
		if (arg2 instanceof LogisticaVO)
		{
			LogisticaVO logistica = (LogisticaVO) arg2;
			return String.valueOf(logistica.getLogisticaID());
		}
		return "";

	}

}
